﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;
using System.Collections.ObjectModel;

public partial class public_html_NewSite_CMSV2_GamesList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["ViewOption"] == null || Request["ViewOption"] == "0")
            {
                this.uiHlReviewAllReviews.Enabled = false;
                this.uiHlReviewNoReview.Enabled = true;

                using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
                using (IReviewService reviewService = ServiceFactory.ReviewService)
                using (IGameService gameService = ServiceFactory.GameService)
                {
                    this.uiBlItems.Items.Clear();
                    Game[] allGames = gameService.GetAll();

                    Dictionary<String, Review> items = new Dictionary<String, Review>();
                    int unknownCounter = 0;
                    foreach (Review review in reviewService.Find())
                    {
                        Game game = allGames.FirstOrDefault(t => t.Id.Equals(review.GameId));
                        if (game == null)
                            items.Add("Ukendt spil #" + (++unknownCounter).ToString(), review);
                        else
                        {
                            try
                            {
                                items.Add(game.Title + " (" + formatService.GetAll().First(t => t.Id.Equals(game.Format)).Name + ")", review);
                            }
                            catch (ArgumentException)
                            {
                                items.Add(game.Title + " (" + formatService.GetAll().First(t => t.Id.Equals(game.Format)).Name + ") - DUBLIKAT: UID#" + review.Id.ToString(), review);
                            }
                            catch (Exception err)
                            {
                                System.Diagnostics.Debug.Write(err.ToString());
                            }
                        }
                    }

                    foreach (KeyValuePair<String, Review> pair in items.OrderBy(t => t.Key))
                    {
                        this.uiBlItems.Items.Add(
                            new ListItem(pair.Key, "ReviewManager.aspx?ReviewId=" + pair.Value.Id)
                            );
                    }
                }
            }
            else if (Request["ViewOption"] == "1")
            {
                this.uiHlReviewAllReviews.Enabled = true;
                this.uiHlReviewNoReview.Enabled = false;

                using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
                using (IReviewService reviewService = ServiceFactory.ReviewService)
                using (IGameService gameService = ServiceFactory.GameService)
                {
                    this.uiBlItems.Items.Clear();
                    Game[] allGames = gameService.GetAll();
                    Collection<Review> allReviews = reviewService.Find();

                    Dictionary<String, Game> items = new Dictionary<String, Game>();
                    foreach (Game game in allGames)
                    {
                        Review review = allReviews.FirstOrDefault(t => t.GameId.Equals(game.Id));
                        if (review == null)
                        {
                            Format format = formatService.GetAll().FirstOrDefault(t => t.Id.Equals(game.Format));
                            items.Add(game.Title + " (" + (format == null ? "Ukendt format" : format.Name) + ")", game);
                        }
                    }

                    foreach (KeyValuePair<String, Game> pair in items.OrderBy(t => t.Key))
                    {
                        this.uiBlItems.Items.Add(
                            new ListItem(pair.Key, "ReviewManager.aspx?GameId=" + pair.Value.Id.ToString())
                            );
                    }
                }
            }
        }
    }
}
